package com.haiermzj.client;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

/**
 * @ClassName: TestSocketCallback
 * @Description: socket接收回调例子
 **/
public class TestSocketCallback implements SocketCallback {

    private static final String TAG = "TestSocketCallback";

    private int connecteTimes = 0;
    private int disconnecteTimes = 0;

    private Handler handler;

    public TestSocketCallback(Handler handler) {
        this.handler = handler;
    }


    @Override
    public void connected() {
        Log.i(TAG, "socket已经连接上了——————");
        connecteTimes++;
        Log.i(TAG, "第 " + connecteTimes + " 次连接回调");
    }


    @Override
    public void disconnect() {
        Log.i(TAG, "socket已经断开了——————");
        disconnecteTimes++;
        Log.i(TAG, "第 " + disconnecteTimes + " 次 断开连接 回调");
    }


    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
    @Override
    public void receive(byte[] buffer) {

//        Bitmap bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length);
//        Log.e(TAG, "socket接收到了数据，请进行处理——————receive: " + bitmap.getByteCount());
//        Log.i(TAG, "socket接收到了数据，请进行处理——————" + buffer.length);

        Message msg = this.handler.obtainMessage();
        msg.what = 1;
        msg.obj = new String(buffer);
//        msg.obj = bitmap;
        handler.sendMessage(msg);

    }

}

 
